using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//for use in D-Pad and button
[RequireComponent(typeof(Character))]
public class ManuelController : MonoBehaviour
{
    private Character character;
    public static ManuelController instance;
    public List<Character.ButtonCommandEnd> commandList = new List<Character.ButtonCommandEnd>();

	// listen to input event;
	protected void Start ()
	{
        instance = this;
        character = GetComponent<Character>();
        character.customInput = KeyBoardInput;
        EasyJoystick.On_JoystickMove += OnJoystickMove;
        EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;		
	}

	void OnJoystickMove(MovingJoystick move)
	{
		Camera cam = Camera.main;
		float directionIndegree = cam.transform.rotation.y + move.Axis2Angle();

        Character.DirectionCommand message = new Character.DirectionCommand();
		message.character = character;
		message.direction = directionIndegree;
        character.onDirectionCommand.SendEventMessage(message);
	}
	
	void OnJoystickMoveEnd(MovingJoystick move)
	{
		Camera cam = Camera.main;
		float directionIndegree = cam.transform.rotation.y + move.Axis2Angle();

        Character.DirectionCommandEnd message = new Character.DirectionCommandEnd();
        message.character = character;
		message.direction = directionIndegree;
		character.onDirectionCommandEnd.SendEventMessage(message);
	}

	// custom update for key board input 
	MovingJoystick lastKeyBoardMove = new MovingJoystick();
	void KeyBoardInput()
	{
		// move direction
		MovingJoystick move = new MovingJoystick();
		if(Input.GetKey(KeyCode.W)) move.joystickAxis.y += 1;
		if(Input.GetKey(KeyCode.S)) move.joystickAxis.y += -1;
		if(Input.GetKey(KeyCode.A)) move.joystickAxis.x += -1;
		if(Input.GetKey(KeyCode.D)) move.joystickAxis.x += 1;

		if(move.joystickAxis.sqrMagnitude != 0)
		{
			OnJoystickMove(move);
            character.SetNullTarget();
		}
		else if(lastKeyBoardMove.joystickAxis.sqrMagnitude != 0)
		{
			OnJoystickMoveEnd(move);
		}

		lastKeyBoardMove = move;

		// attack input
		if(Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.U)) 
		{
            if(character.onButtonCommand.listened)
            {
                AttackInput();
            }
		}
		if(Input.GetMouseButtonUp(1) || Input.GetKeyUp(KeyCode.U)) 
		{
            if(character.onButtonCommandEnd.listened)
            {
                AttackEndInput();
            }
		}
        if(Input.GetKeyUp(KeyCode.I)) 
        {
            if(character.onButtonCommandEnd.listened)
            {
                WeaponSkillInput();
            }
        }
        if(Input.GetKeyUp(KeyCode.O)) 
        {
            if(character.onButtonCommandEnd.listened)
            {
                EvadeInput();
            }
        }
        if(commandList.Count> 0)
        {
            PopUpAndExecuteCommand();
        }
	}

    public void AttackInput()
    {
        Character.ButtonCommand message = new Character.ButtonCommand();
        message.character = character;
        message.button = ButtonType.Attack;
        character.onButtonCommand.SendEventMessage(message);
    }

    public void AttackEndInput()
    {
        Character.ButtonCommandEnd message = new Character.ButtonCommandEnd();
        message.character = character;
        message.button = ButtonType.Attack;
        character.onButtonCommandEnd.SendEventMessage(message);
    }

    public void EvadeInput()
    {
        Character.ButtonCommandEnd message = new Character.ButtonCommandEnd();
        message.character = character;
        message.button = ButtonType.Evade;
        character.onButtonCommandEnd.SendEventMessage(message);
    }

    public void WeaponSkillInput()
    {
        Character.ButtonCommandEnd message = new Character.ButtonCommandEnd();
        message.character = character;
        message.button = ButtonType.WeaponSpecial;
        character.onButtonCommandEnd.SendEventMessage(message);
    }

    void PopUpAndExecuteCommand()
    {
        Character.ButtonCommandEnd message = commandList[0];
        character.onButtonCommandEnd.SendEventMessage(message);
        commandList.Remove(message);
    }

    public void AddCommand(ButtonType type)
    {
        Character.ButtonCommandEnd message = new Character.ButtonCommandEnd();
        message.character = character;
        message.button = type;
        commandList.Add(message);
    }
}


